package com.sanyang.pda.ui.scancodequery;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sanyang.pda.adapter.StockListAdapter;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.StockBean;
import com.sanyang.pda.event.FinishBean;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.ui.mvp.base.ui.BaseActivity;
import com.wangshen.projectbase.R;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class StockListActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.title)
    FrameLayout title;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.scan)
    ScanButView scan;
    private String code;
    StockListAdapter adapter;
    List<StockBean> stockBeans=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stocklist);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        rvList.setLayoutManager(new LinearLayoutManager(this));
        adapter=new StockListAdapter(R.layout.item_stock_list,stockBeans);
        rvList.setAdapter(adapter);
        code=getIntent().getExtras().getString(StringConfig.CODE);
        getData();
        tvCode.setText("库位码："+code);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle=new Bundle();
                bundle.putString(StringConfig.CODE,stockBeans.get(position).getSequenceNo());
                Intent intent=new Intent(StockListActivity.this,StockDetailActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public int getView() {
        return R.layout.activity_stocklist;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {

    }

    @Subscribe
   public void event(FinishBean finishBean){
        finish();
   }
    /*private void getData(){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("storagebinId",code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), jsonObject.toString());
        *//*RetrofitUtils.getApiUrl().getWarehouseStockList(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseBean<List<StockBean>>>() {
                    @Override
                    public void accept(BaseBean<List<StockBean>> agvDetailRespose) throws Exception {
                        if (agvDetailRespose.getCode() == 200) {
                            stockBeans.addAll(agvDetailRespose.getData());
                            adapter.notifyDataSetChanged();
                        }else {
                            Toast.makeText(StockListActivity.this, agvDetailRespose.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.e("AGVInActivity", throwable.getMessage());
                    }
                });*//*
    }*/
    private void getData(String storagebinId){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("storagebinId",storagebinId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), jsonObject.toString());
        /*RetrofitUtils.getApiUrl().getWarehouseStockList(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseBean<List<StockBean>>>() {
                    @Override
                    public void accept(BaseBean<List<StockBean>> agvDetailRespose) throws Exception {
                        if (agvDetailRespose.getCode() == 200) {
                            if (agvDetailRespose.getData().size() == 0){
                                Toast.makeText(StockListActivity.this, "无数据", Toast.LENGTH_SHORT).show();
                            }else {
                                Bundle bundle=new Bundle();
                                bundle.putString(StringConfig.CODE,storagebinId);
                                Intent intent=new Intent(StockListActivity.this,StockListActivity.class);
                                intent.putExtras(bundle);
                                startActivity(intent);
                                finish();
                            }
                        }else {
                            Toast.makeText(StockListActivity.this, agvDetailRespose.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.e("AGVInActivity", throwable.getMessage());
                    }
                });*/
    }
    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        scan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                if (scanBean.getType() == 2){
                    getData(scanBean.getCode());
                }else {
                    Toast.makeText(StockListActivity.this, "请扫正确的二维码", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        scan.setScanCallBack(null);
    }
}
